﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Configuration;
using MyLib.Integration;

namespace MyLib.Web.HttpModule
{
    public class HeaderInclude : IHttpModule
    {
        public void Dispose()
        {

        }

        public void Init(HttpApplication context)
        {
            context.PostMapRequestHandler += new EventHandler(context_PostMapRequestHandler);
        }

        void context_PostMapRequestHandler(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;

            HttpContext context = application.Context;

            if (context.CurrentHandler is Page)
            {
                Page page = context.CurrentHandler as Page;
                page.PreRenderComplete += new EventHandler(page_PreRenderComplete);
                
            }
        }        

        void page_PreRenderComplete(object sender, EventArgs e)
        {
            Page page = sender as Page;
            string prefix = SSOUtil.PortalUrl;
            if (string.IsNullOrEmpty(prefix))
            {
                prefix = "..";
            }
            //css
            string css= prefix + "/Content/skin/bootstrap/css/";
            HtmlLink link = new HtmlLink();
            link.Href = css + "bootstrap.min.css";  
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("rel", "stylesheet");
            page.Header.Controls.Add(link);

            link = new HtmlLink();
            link.Href = css + "bootstrap-responsive.min.css";  
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("rel", "stylesheet");
            page.Header.Controls.Add(link);

            link = new HtmlLink();
            link.Href = css + "bootstrap-custom.css";
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("rel", "stylesheet");
            page.Header.Controls.Add(link);

            //js
            string js=prefix + "/Content/js/";
            LiteralControl lit = new LiteralControl();
            lit.Text = "<script src=\"" + js + "jquery.min.js\" type=\"text/javascript\"></script>";
            page.Header.Controls.Add(lit);

            lit = new LiteralControl();
            lit.Text = "<script src=\"" + js + "bootstrap.min.js\" type=\"text/javascript\"></script>";
            page.Header.Controls.Add(lit);

            lit = new LiteralControl();
            lit.Text = "<script src=\"" + js + "My97DatePicker/WdatePicker.js\" type=\"text/javascript\"></script>";
            page.Header.Controls.Add(lit);
        }

    }
}
